package com.example.travel.service;
import com.example.travel.mapper.FriendsUserMapper;
import com.example.travel.mapper.UserMapper;
import com.example.travel.pojo.orm.FriendsUser;
import com.example.travel.pojo.orm.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class FriendUserService {
    @Autowired
    private FriendsUserMapper friendMapper;
    @Autowired
    private UserMapper userMapper;
    public List<FriendsUser> getFriendsUsersById(int userId){
        return friendMapper.getFriendsByUserId(userId);
    }

    public List<User> getFriendsByUserId(int userId){
        List<FriendsUser> friendUsers = this.getFriendsUsersById(userId);
        List<User> users = new ArrayList<>();
        for (FriendsUser friendUser : friendUsers) {
            if (userId==friendUser.getUserId()){
                users.add(userMapper.getUserById(friendUser.getFriendId()));
            }else {
                users.add(userMapper.getUserById(friendUser.getUserId()));
            }
        }
        return users;
    }
    public boolean addFriendUser(int userId,int friendId){
        FriendsUser friendUser = friendMapper.getFriendUser(userId, friendId);
        if (friendUser==null){
            FriendsUser friendsUser = new FriendsUser(userId, friendId);
            int result = friendMapper.addFriendUser(friendsUser);
            return result == 1;
        }else {
            return false;
        }

    }
}
